# !/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__project__ =  'tricker'
__file__    =  '__init__.py.py'
__author__  =  'king'
__time__    =  '2024/5/21 上午9:17'


                              _ooOoo_
                             o8888888o
                             88" . "88
                             (| -_- |)
                             O\  =  /O
                          ____/`---'\____
                        .'  \\|     |//  `.
                       /  \\|||  :  |||//  \
                      /  _||||| -:- |||||-  \
                      |   | \\\  -  /// |   |
                      | \_|  ''\---/''  |   |
                      \  .-\__  `-`  ___/-. /
                    ___`. .'  /--.--\  `. . __
                 ."" '<  `.___\_<|>_/___.'  >'"".
                | | :  `- \`.;`\ _ /`;.`/ - ` : | |
                \  \ `-.   \_ __\ /__ _/   .-` /  /
           ======`-.____`-.___\_____/___.-`____.-'======
                              `=---='
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       佛祖保佑        永无BUG
"""
import os.path
import uuid

import click

from tricker.commands.tacker.models.vnf import (
    Entity,
    Image,
    Flavor,
    Server,
    VNF,
    Network,
)
from tricker.commands.tacker.worker import Worker
from tricker.common.log import LOG


@click.group(name='tacker', help='OpenStack Tacker Trickers')
def group():
    pass


@group.command(name='test', help='Test Connection')
def test():
    _worker = Worker()
    LOG.info("endpoints: %s" % _worker.client.endpoints)


@group.command(name='init', help='Quick Init VNF Instances')
@click.option(
    '--csar',
    default=None,
    help='The CSAR file, default is None',
)
@click.option(
    '--data',
    default=None,
    help='The request json file, default is None',
)
def init(csar: str, data: str) -> int:
    if csar:
        if not os.path.isfile(csar):
            LOG.error(f'file {csar} not found')
            return 1
    if data:
        if not os.path.isfile(data):
            LOG.error(f'file {csar} not found')
            return 1
    _worker = Worker()
    _worker.run(csar, data)
    return 0


@group.command(name='clean', help='Quick Clean All Tacker Resources')
def clean():
    _worker = Worker()
    _worker.run_clean()


@group.command(
    name='create_csar', help='Quick Create CSAR File And Request File'
)
@click.option(
    '--image',
    default='cirros',
    help='The name/ID of openstack image, default is cirros',
    metavar='<image name/ID>',
)
@click.option(
    '--image-version',
    default='v1.0',
    help='The version of openstack image, default is v1.0',
    metavar='<str>',
)
@click.option(
    '--image-hash',
    default='',
    help='The hash of openstack image file base on sha-512, default is None',
    metavar='<str>',
)
@click.option(
    '--min-disk',
    default=0,
    help='The minimum size disk of openstack image, default is 0',
    metavar='<int>',
)
@click.option(
    '--image-size',
    default=0,
    help='The minimum size disk of openstack image, default is 0',
    metavar='<int>',
)
@click.option(
    '--container-format',
    default='bare',
    help='The container format of openstack image, default is bare',
    metavar='<str>',
)
@click.option(
    '--disk-format',
    default='qcow2',
    help='The disk format of openstack image, default is qcow2',
    metavar='<str>',
)
@click.option(
    '--flavor',
    default='m1.tiny',
    help='The name/ID of openstack flavor, default is m1.tiny',
    metavar='<flavor name/ID>',
)
@click.option(
    '--memory',
    default=512,
    help='The memory size of openstack flavor, default is 512MB',
    metavar='<int>',
)
@click.option(
    '--vcpu',
    default=1,
    help='The vcpu of openstack flavor, default is 1',
    metavar='<int>',
)
@click.option(
    '--disk',
    default='1',
    help='The disk of openstack flavor, default is 1GB',
    metavar='<int>',
)
@click.option(
    '--network',
    default='internalVL',
    help='The name of VNF network, default is internalVL',
    metavar='<str>',
)
@click.option(
    '--server',
    default='VDU',
    help='The name of openstack servers to create, default is VDU',
    metavar='<str>',
)
@click.option(
    '--max-count',
    default=3,
    help='The maximum count of openstack servers to create, default is 3',
    metavar='<int>',
)
@click.option(
    '--min-count',
    default=0,
    help='The minimum count of openstack servers to create, default is 0',
    metavar='<int>',
)
@click.option(
    '--vnf-type',
    default='UOS.Provider.VNF',
    help='The name of customized VNF type, default is UOS.Provider.VNF',
    metavar='<str>',
)
@click.option(
    '--vnfd',
    default=None,
    help='The uuid of VNFD, if not be given, '
    'will automatically generate using uuid4',
    metavar='<str>',
)
@click.option(
    '--vnf-version',
    default='v1.0',
    help='The version of VNF, default is v1.0',
    metavar='<str>',
)
@click.option(
    '--provider',
    default='UnionTech Software Technology',
    help='The provider of VNF, default is UnionTech Software Technology',
    metavar='<str>',
)
@click.option(
    '--product',
    default='UOS VNF',
    help='The product name of VNF, default is UnionTech Software Technology',
    metavar='<str>',
)
@click.option(
    '--software-version',
    default='v1.0',
    help='The software version of VNF, default is v1.0',
    metavar='<str>',
)
@click.option(
    '--vnf-flavour',
    default=None,
    help='The uuid of VNF flavour, if not be given, '
    'will automatically generate using uuid4',
    metavar='<str>',
)
@click.option(
    '--vnf-flavour-description',
    default=None,
    help='The description of VNF flavour, default is None',
    metavar='<str>',
)
@click.option(
    '--dev',
    is_flag=True,
    help='Turn on dev mode, default is False',
)
def create_csar(
    image: str,
    image_version: str,
    image_hash: str,
    min_disk: int,
    image_size: int,
    container_format: str,
    disk_format: str,
    flavor: str,
    memory: int,
    vcpu: int,
    disk: int,
    network: str,
    server: str,
    max_count: int,
    min_count: int,
    vnf_type: str,
    vnfd: str,
    vnf_version: str,
    provider: str,
    product: str,
    software_version: str,
    vnf_flavour: str,
    vnf_flavour_description: str,
    dev: bool,
):
    if not vnfd:
        vnfd = uuid.uuid4()
        LOG.info(f'generate VNFD {vnfd}')
    if not vnf_flavour:
        vnf_flavour = uuid.uuid4()
        LOG.info(f'generate VNF flavour ID {vnf_flavour}')
    entity = Entity(
        image=Image(
            id=image,
            version=image_version,
            hash=image_hash,
            min_disk=min_disk,
            size=image_size,
            container_format=container_format,
            disk_format=disk_format,
        ),
        flavor=Flavor(id=flavor, mem=memory, vcpu=vcpu, disk=disk),
        network=Network(
            name=network, connect_point='CP', cidr='192.168.0.0/24'
        ),
        server=Server(name=server, min_count=min_count, max_count=max_count),
        VNF=VNF(
            name=vnf_type,
            VNFD=vnfd,
            version=vnf_version,
            provider=provider,
            product=product,
            software_version=software_version,
            flavour_id=vnf_flavour,
            flavour_description=vnf_flavour_description,
        ),
    )
    _worker = Worker()
    _worker.maker_casr(entity, dev)
